﻿using Hotel.BLL;
using Hotel.DAL;
using Hotel.Model;
using MD5Hash;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Cryptography;

namespace Hotel.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        public LoginController(LoginBLL loginBLL)
        {
            LoginBLL = loginBLL;
        }

        public LoginBLL LoginBLL { get; }

        [HttpPost]
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        public int Add(Login login)
        {
            try
            {
                return LoginBLL.Add(login);
            }
            catch (Exception)
            {

                throw;
            }
        }

        [HttpGet]
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int  Login(string name,string pwd)
        {
            try
            {
                var user= LoginBLL.Login(name);
                if (user==null)
                {
                    return -1;
                }
                else if (user.Pwd.GetMD5()==pwd.GetMD5())
                {
                    return 1;
                }
                else
                {
                    return 0;
                }

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
